# -*- coding: utf-8 -*
__author__ = 'dell'
from operator import itemgetter, attrgetter
import math
inputE = open('5.txt', 'r') #外卖订单
inputB = open('2.txt', 'r') #配送点经纬度
ouputE = open('5_2.txt', 'w') #外卖订单
inputC = open('3.txt','r') #商家经纬度
listE ={}
listE0 ={}
listE1 ={}
listE2={}
listE3={}
listE4={}


L={}
S=[]
S0=[]
S1=[]
S2=[]
S3=[]
S4=[]

for eachline in inputC.readlines():
    temp = eachline.strip().split(',')
    S.append([temp[0],[float(temp[1]),float(temp[2])]])
    S0.append([temp[0],[float(temp[1]),float(temp[2])]])
    S1.append([temp[0],[float(temp[1]),float(temp[2])]])
    S2.append([temp[0],[float(temp[1]),float(temp[2])]])
    S3.append([temp[0],[float(temp[1]),float(temp[2])]])
    S4.append([temp[0],[float(temp[1]),float(temp[2])]])
    L[temp[0]] = [float(temp[1]),float(temp[2])]
for eachline in inputB.readlines():
    temp = eachline.strip().split(',')
    L[temp[0]] = [float(temp[1]),float(temp[2])]

def caldistance(point1,point2):
    dlon = (point1[0]-point2[0])/2
    dlat = (point1[1]-point2[1])/2
    lat1 = point1[1]
    lat2 = point2[1]
    R = 6378137
    s= 2*R*math.asin(((math.sin(dlat*math.pi/180))**2+math.cos(lat1*math.pi/180)*math.cos(lat2*math.pi/180)*(math.sin(dlon*math.pi/180))**2)**0.5)
    return s

for eachline in inputE.readlines(): #改成字典形式
    temp = eachline.strip().split(',')
    time1 = temp[3].split(':')
    temp[3] = int(time1[0])*60+int(time1[1])-480
    time2 = temp[4].split(':')
    temp[4] = int(time2[0])*60+int(time2[1])-480
    temp[5] = int(temp[5])
    disTime = int(round(caldistance(L[temp[1]],L[temp[2]])/250))
    if disTime>180:
        if temp[2] in listE0:listE0[temp[2]].append(temp)
        else:
            listE0[temp[2]]=[]
            listE0[temp[2]].append(temp)
    elif disTime<=180 and disTime>150:
        if temp[2] in listE1:listE1[temp[2]].append(temp)
        else:
            listE1[temp[2]]=[]
            listE1[temp[2]].append(temp)
    elif disTime<=150 and disTime>90:
        if temp[2] in listE2:listE2[temp[2]].append(temp)
        else:
            listE2[temp[2]]=[]
            listE2[temp[2]].append(temp)
    elif disTime<=90 and disTime>60:
        if temp[2] in listE3:listE3[temp[2]].append(temp)
        else:
            listE3[temp[2]]=[]
            listE3[temp[2]].append(temp)

    elif disTime<=60 and disTime>30:
        if temp[2] in listE4:listE4[temp[2]].append(temp)
        else:
            listE4[temp[2]]=[]
            listE4[temp[2]].append(temp)
    else:
        if temp[2] in listE:listE[temp[2]].append(temp)
        else:
            listE[temp[2]]=[]
            listE[temp[2]].append(temp)



def staytimeCal(p):
    time = int(round(3*(p**0.5)+5))
    return time

def calCost(Etotal):
    cost = 0
    timeNow = Etotal[0][3]
    if len(Etotal)>0:
        for i in range(len(Etotal)-1):
            disTime = int(round(caldistance(L[Etotal[i][2]],L[Etotal[i+1][2]])/250))
            timeNow +=disTime
            cost +=disTime
            cost += max(0,(timeNow-Etotal[i+1][3])*5)
            timeNow = max(Etotal[i+1][3],timeNow)
    startS = Etotal[-1][2]
    timeNow += int(round(caldistance(L[Etotal[0][1]],L[startS])/250))
    cost = cost+int(round(caldistance(L[Etotal[0][1]],L[startS])/250))+max(0,(timeNow-Etotal[0][4])*5)
    if len(Etotal)>0:
        for i in range(1,len(Etotal)):
            travelTime = int(round(caldistance(L[Etotal[i][1]],L[Etotal[i-1][1]])/250))
            timeNow +=travelTime
            cost +=travelTime
            cost +=max(0,(timeNow-Etotal[i][4])*5)
            timeNow += staytimeCal(Etotal[i][-1])
    return cost


def JL(S,listE,jlt,saveTimeStand):
    totalSaveTime = 0
    S.sort()
    newS=[[S[0][0]]]
    S.pop(0)
    while len(S)>0:
        add = False
        for each in newS:
            minTime = max([int(round(caldistance(L[x],L[S[0][0]])/250)) for x in each])
            if minTime<jlt:
                add = True
                each.append(S[0][0])
                break
        if add==False:
            newS.append([S[0][0]])
        S.pop(0)
    for eachnewS in newS:
        eachE_Set = []
        for eachS in eachnewS:
            if eachS in listE:
                eachE_Set += listE[eachS]
        if eachE_Set==[]:pass
        else:
            eachE_Set.sort(key=itemgetter(3))
            E_set =[[eachE_Set[0]]]
            cost = [calCost([eachE_Set[0]])]
            packet = [eachE_Set[0][-1]]
            eachE_Set.pop(0)
            while len(eachE_Set)>0:
                addSuccess = False
                for i in range(len(E_set)):
                    j = i
                    saveTime = cost[j] + calCost([eachE_Set[0]])-calCost(E_set[j]+[eachE_Set[0]])
                    if eachE_Set[0][-1]+packet[j]<=140 and saveTime>saveTimeStand:
                        E_set[j].append(eachE_Set[0])
                        cost[j] = calCost(E_set[j])
                        packet[j] +=eachE_Set[0][-1]
                        totalSaveTime+=saveTime
                        addSuccess =True
                        break
                if addSuccess==False:
                    E_set.append([eachE_Set[0]])
                    cost.append(calCost([eachE_Set[0]]))
                    packet.append(eachE_Set[0][-1])
                eachE_Set.pop(0)
            print E_set
            for eachnewE in E_set:
                newline0 =[eachnewE[0][2]]
                newline1= [eachnewE[0][0]]
                newline2 =[eachnewE[0][-1]]
                newline3= [eachnewE[0][3],eachnewE[0][3]]
                timeNow = int(eachnewE[0][3])
                for i in range(1,len(eachnewE)):
                    newline0.append(eachnewE[i][2])
                    newline1.append(eachnewE[i][0])
                    newline2.append(int(eachnewE[i][-1]))
                    SdisT = int(round(caldistance(L[eachnewE[i][2]],L[eachnewE[i-1][2]])/250))
                    timeNow +=SdisT
                    newline3.append(timeNow)
                    if timeNow<int(eachnewE[i][3]):
                        timeNow = int(eachnewE[i][3])
                    newline3.append(timeNow)

                startS = newline0[-1]
                newline0.append(eachnewE[0][1])
                newline1.append(eachnewE[0][0])
                newline2.append(-int(eachnewE[0][-1]))
                dSB = int(round(caldistance(L[startS],L[eachnewE[0][1]])/250))
                timeNow +=dSB
                newline3.append(timeNow)
                timeNow +=staytimeCal(int(eachnewE[0][-1]))
                newline3.append(timeNow)
                for i in range(1,len(eachnewE)):
                    newline0.append(eachnewE[i][1])
                    newline1.append(eachnewE[i][0])
                    newline2.append(-int(eachnewE[i][-1]))
                    SdisT = int(round(caldistance(L[eachnewE[i][1]],L[eachnewE[i-1][1]])/250))
                    timeNow +=SdisT
                    newline3.append(timeNow)
                    timeNow +=staytimeCal(int(eachnewE[i][-1]))
                    newline3.append(timeNow)
                for one in newline0:
                    ouputE.write('%s\t' %one)
                ouputE.write('\n')
                for one in newline1:
                    ouputE.write('%s\t' %one)
                ouputE.write('\n')
                for one in newline2:
                    ouputE.write('%s\t' %one)
                ouputE.write('\n')
                for one in newline3:
                    ouputE.write('%s\t' %one)
                ouputE.write('\n')

    return totalSaveTime

print JL(S,listE,50,10)+JL(S0,listE0,50,10)+JL(S1,listE1,50,10)+JL(S2,listE2,50,5)++JL(S3,listE3,50,5)+JL(S4,listE4,50,5)

inputE.close()
inputC.close()
ouputE.close()
inputB.close()